package com.qs.controller;

import com.qs.ConStants;
import com.qs.commen.Response;
import com.qs.pojo.SysLoginLog;
import com.qs.pojo.SysUser;
import com.qs.utils.DateUtils;
import com.qss.service.LoginLogService;
import com.qss.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RestController
public class LoginController {

    @Reference
    private UserService userService;
    @Reference
    private LoginLogService loginLogService;

    @PostMapping("/login")
    public Response login(SysUser user, HttpSession session,String code){
        String mcode = session.getAttribute(ConStants.KAPTCHA_KEY).toString();
        if(!code.equals(mcode)){
            return new Response(602,"验证码错误");
        }
        Response data  = userService.checkLogin(user);
        if(data.getCode()==200) {
            user = userService.queryUserByUserName(user.getLoginName());
            session.setAttribute(ConStants.CURR_USER, user);
        }
        SysLoginLog log=new SysLoginLog();
        log.setUserId(user.getUserId());
        log.setLoginIp(user.getAddress());
        log.setLoginDate(DateUtils.Date("yyyy-MM-dd HH:mm:ss"));
        loginLogService.saveLogin(log);
        return data;


    }
}
